package src.queries;

import java.sql.*;


public class DBConnector{

	private Connection m_con;
	private boolean is_connected;
	
	//String m_url = "jdbc:mysql://127.0.0.1:3306/cmput_391";
	//String m_driverName = "com.mysql.jdbc.Driver";
	
	String m_url = "jdbc:oracle:thin:@gwynne.cs.ualberta.ca:1521:CRS";
	String m_driverName = "oracle.jdbc.driver.OracleDriver";
	
	
	private String m_userName = "jjlee2";
	private String m_password = "jjllee22";

	private PreparedStatement commit;

	public DBConnector() {}
	
	public void connect() 
	throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException {
		String m_url = "jdbc:oracle:thin:@gwynne.cs.ualberta.ca:1521:CRS";
		String m_driverName = "oracle.jdbc.driver.OracleDriver";

		//String m_url = "jdbc:mysql://127.0.0.1:3306/cmput_391";
		//String m_driverName = "com.mysql.jdbc.Driver";		
		
		Class<?> drvClass = Class.forName(m_driverName);
		DriverManager.registerDriver((Driver) drvClass.newInstance());
		m_con = DriverManager.getConnection(m_url, m_userName, m_password);
		is_connected = true;

	}

	public void disconnect() {
		if (is_connected)
			try {
				commit = m_con.prepareStatement("commit");
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				commit.executeQuery();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			try {
				m_con.close();
			} catch (SQLException ex) {
				ex.printStackTrace();
			}
	}

	public Connection getConnection() throws ClassNotFoundException, SQLException, InstantiationException, IllegalAccessException{
		if(m_con == null){
			connect();
		}
		return m_con;
	}

}
